﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MyMes.Dto;
using MyMes.Infrastructure;
using MyMES.Dto;
using MyMES.Infrastructure;
using MyMES.IServices.Inventorytasksment;
using VOL.Core.Extensions;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels.Core;
using VOL.Entity.DomainModels.Repository;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace MyMES.Services.Inventorytasksment
{
    public class InventorytasksServices:IInventorytasksServices
    {
        private readonly IBaseRepository<Inventorytasks> _inventorytasksRepository;

        public InventorytasksServices(IBaseRepository<Inventorytasks> inventorytasksRepository)
        {
            _inventorytasksRepository = inventorytasksRepository;
        }

       

        public async Task<int> CreateInventorytasks(Inventorytasks inventorytasks)
        {
           
                inventorytasks.States = 1;
                await _inventorytasksRepository.AddAsync(inventorytasks);
                return 1;
         
          
        }

        public async Task<PageGridData<Inventorytasks>> InventorytasksList(GetInventorytasksDTO inventorytasksDTO, int PageIndex, int PageSize)
        {
            PageGridData<Inventorytasks> pageData = new PageGridData<Inventorytasks>();
            // 构建查询
            var list = _inventorytasksRepository.GetAll();
            // 使用WhereIf方法进行条件查询
            list = list.WhereIF(!string.IsNullOrEmpty(inventorytasksDTO.TaskCode),
                                 x => x.TaskCode.Contains(inventorytasksDTO.TaskCode));
            list = list.WhereIF(!string.IsNullOrEmpty(inventorytasksDTO.TaskName),
                                x => x.TaskName.Contains(inventorytasksDTO.TaskName));
            // 获取总记录数
            pageData.total = await list.CountAsync();

            // 分页处理
            var data = await list.OrderByDescending(a => a.Id)
                                 .Skip((PageIndex - 1) * PageSize)
                                 .Take(PageSize)
                                 .ToListAsync();

            pageData.rows = data;

            return pageData;
        }

        public async Task<PageGridData<Inventorytasks>> QueryInventorytasks(InventorytasksDTO inventorytasksDTO, int PageIndex, int PageSize)
        {
            PageGridData<Inventorytasks> pageData = new PageGridData<Inventorytasks>();
            // 构建查询
            var list =  _inventorytasksRepository.GetAll();

            // 使用WhereIf方法进行条件查询
            list = list.WhereIF(!string.IsNullOrEmpty(inventorytasksDTO.InventoryCode),
                                 x => x.InventoryCode.Contains(inventorytasksDTO.InventoryCode));
            list = list.WhereIF(!string.IsNullOrEmpty(inventorytasksDTO.InventoryName),
                                x => x.InventoryName.Contains(inventorytasksDTO.InventoryName));
            list = list.WhereIF(inventorytasksDTO.States > 0, x => x.States == inventorytasksDTO.States);


            // 日期查询 - 如果有日期查询条件，则查询当天
            if (inventorytasksDTO.InventoryTime.HasValue)
            {
                DateTime startDate = inventorytasksDTO.InventoryTime.Value.Date;
                DateTime endDate = startDate.AddDays(1).AddSeconds(-1);
                list = list.Where(a => a.InventoryTime >= startDate && a.InventoryTime <= endDate);
            }

            // 获取总记录数
            pageData.total = await list.CountAsync();

            // 分页处理
            var data = await list.OrderByDescending(a => a.Id)
                                 .Skip((PageIndex - 1) * PageSize)
                                 .Take(PageSize)
                                 .ToListAsync();

            pageData.rows = data;

            return pageData;

        }

        public async Task<WebResponseContent> DelInventorytasks(string ids)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                if (string.IsNullOrEmpty(ids))
                {
                    return response.Error("未选择需要删除的记录");
                }

                // 将逗号分隔的字符串转换为整数ID列表
                List<int> idList = new List<int>();
                foreach (var id in ids.Split(','))
                {
                    if (int.TryParse(id.Trim(), out int parsedId))
                    {
                        idList.Add(parsedId);
                    }
                }

                if (idList.Count == 0)
                {
                    return response.Error("无效的ID格式");
                }
                // 执行批量删除
                await _inventorytasksRepository.DelAll(a => idList.Contains(a.Id));

                return response.OK($"删除成功");
            }
            catch (Exception ex)
            {
                return response.Error($"批量删除到货通知单失败：{ex.Message}");
            }
        }
    }
}
